//
//  WEXTabBar.swift
//  microBlog
//
//  Created by apple on 16/4/16.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit



class WEXTabBar: UITabBar {
    
    var didClickButtonNewContent: (()->())?
    
    // MARK: - 初始化界面
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(addContentButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置子控件位置
    override func layoutSubviews() {
        super.layoutSubviews()
        addContentButton.center = CGPointMake(self.frame.size.width / 2 , self.frame.size.height / 2)
        
        let itemWidth = UIScreen.mainScreen().bounds.width / 5
        var index = 0
        
        //循环遍历子view
        for value in subviews {
            //判断value的属性是不是 UITabBarButton 类型
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = itemWidth
                
                value.frame.origin.x = CGFloat(index) * itemWidth
                index++
                
                if index == 2{
                    index++
                    
                }
                
            }
            
        }

    }
    
    // MARK: - 点击事件实现方法
   @objc private func didClickButtnContent() {
        
        didClickButtonNewContent?()
    }
    
    // MARK: - 懒加载按钮控件
    private lazy var addContentButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "didClickButtnContent", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置按钮状态
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        button.sizeToFit()
        
        return button
    }()
    
    
}
